package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.AdvertCollectInfoComponent;
import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.AdvertCollectInfo;
import com.dy.lock.screen.pojo.AdvertOperatorInfo;
import com.dy.lock.screen.pojo.AdvertTagRelationInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.AdvertCollectService;
import com.dy.lock.screen.service.AdvertOperatorService;
import com.dy.lock.screen.service.AdvertService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.utils.DateUtil;
import com.dy.lock.screen.vo.AdCollectResultVo;
import com.dy.lock.screen.vo.AdvertCollectVo;
import com.google.gson.Gson;

@Component("advertCollectInfoComponent")
public class AdvertCollectInfoComponentImpl implements
		AdvertCollectInfoComponent {
	private static final Logger log = Logger
			.getLogger(AdvertCollectInfoComponentImpl.class);
	private Gson gson;

	@Resource(name = "advertCollectService")
	private AdvertCollectService advertCollectService;
	@Resource(name = "advertService")
	private AdvertService advertService;
	@Resource(name = "advertOperatorService")
	private AdvertOperatorService advertOperatorService;

	public AdvertCollectInfoComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<AdCollectResultVo> list(String body, Head head) {
		AdvertCollectVo vo = gson.fromJson(body, AdvertCollectVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("userId", vo.getUserId());
			List<AdvertCollectInfo> adCollectList = this.advertCollectService
					.queryPage(paraMap, head);
			List<AdCollectResultVo> collectList = new ArrayList<AdCollectResultVo>();
			if (adCollectList != null && !adCollectList.isEmpty()) {
				for (AdvertCollectInfo collect : adCollectList) {
					AdCollectResultVo coReVo = new AdCollectResultVo();
					coReVo.setAdId(collect.getAdId());
					coReVo.setAdTitle(collect.getAdTitle());
					coReVo.setAdLinkUrl(collect.getLeftSlideUrl());
					coReVo.setMerchantId(collect.getCompanyId());
					coReVo.setMerchantName(collect.getCompanyName());
					coReVo.setMerchantPortrait(CommonUtil.getUrlPrefix()
							+ collect.getCompanyLogo());
					List<AdvertTagRelationInfo> tags = this.advertService
							.queryByAdId(collect.getAdId(), head);
					if (tags != null && !tags.isEmpty()) {
						coReVo.setAdIconIndex(tags.get(0).getAdTagId());
					}
					String[] picPaths = collect.getAdPicPath().split(";");
					if (picPaths != null) {
						String[] adUrls = new String[picPaths.length];
						for (int j = 0; j < picPaths.length; j++) {
							adUrls[j] = CommonUtil.getUrlPrefix() + picPaths[j];
						}
						coReVo.setAdUrls(adUrls);
					}
					coReVo.setCollectTime(DateUtil.formatTime(collect
							.getCollectTime()));
					collectList.add(coReVo);
				}
				return collectList;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",获取收藏广告列表时，查询收藏广告列表集为空。");
				return new ArrayList<AdCollectResultVo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",获取收藏广告列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public AdvertCollectVo deleteCollectAd(String body, Head head) {
		AdvertCollectVo vo = gson.fromJson(body, AdvertCollectVo.class);
		if (vo != null) {
			String userId = vo.getUserId();
			String adId = vo.getAdId();
			int re = this.advertCollectService.delete(adId, userId, head);
			if (re > 0) {
				AdvertOperatorInfo adOpt = new AdvertOperatorInfo();
				adOpt.setAdId(adId);
				adOpt.setUserId(userId);
				adOpt.setAdScore(0);
				adOpt.setOperatorType(Constant.ADVERT_OPERATE_CANCEL_COLLECT);
				this.advertOperatorService.save(adOpt, head);
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取收藏广告列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
